package com.hc;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class WorldController {
    //@RequestBody 可尝试将请求的字符串数据转化成java对象
    //@ResponseBody 可自动尝试转化成json格式,如果直接返回String则不尝试
    /*
    * Model可选对象,SpringMVC会自动注入该值，用于绑定数据
    *
    * */
    @RequestMapping("/world") //SpringMVC注解,该注解用于绑定一个请求并处理
    public String f1(Model model){
        model.addAttribute("msg","Hello Spring MVC!!!");
        return "world"; //直接返回String,则表示直接加载对应视图文件
    }

    @RequestMapping("/test")
    public String f2(Model model){
        model.addAttribute("msg","Hello Spring MVC!!!");
        int a = 10/0;
        return "world";
    }

    @RequestMapping("/upload")
    public String upload(HttpServletRequest request, MultipartFile file) throws IOException {
        //文件目录创建...
//        String path = "";
//        File nf = new File(path);
//        file.transferTo(nf);
        return "文件上传成功";
    }
    @InitBinder //此方法的执行早于请求的处理方法
    public void transferDate(ServletRequestDataBinder binder){
        CustomDateEditor customDateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-mm-dd"), true);
        binder.registerCustomEditor(Date.class,customDateEditor);
    }
    //接收参数方式1
    public String f3(HttpServletRequest request,Model model){
        request.getAttribute("username");
        return "world";
    }
    //接收参数方式2
    /*
    * @PathVariable当restful路径中变量与参数变量名相同时，默认值可省略
    * 否则，可定义值将路径中对应变量注入到参数变量
    * */
    @RequestMapping("/test/{username}/{password}") //
    public String f3(@PathVariable(required = false) String username, @PathVariable(value = "password",required = false)  String pwd, Model model){
        System.out.println("username:"+username);
        System.out.println("password:"+pwd);
        return "world";
    }
    @RequestMapping("/test2") //可接收如/test2 /test2?name=lisi请求
    public String f3(@RequestParam(value = "name",defaultValue = "zhangsan") String username, Model model){
        System.out.println("username:"+username);
        return "world";
    }
    //接收参数方式3
    public String f3(User user,Model model){
        return "world";
    }
    //Jackson常用注解 用在javabean对象上
    //@JsonProperty 重新定义字段名返回
    //@JsonIgnore 过滤掉字段不返回
    //@JsonFormat 将时间转化成指定格式返回
    //@JsonInclude 将符合条件的字段返回
    //@JsonSerialize 自定义规则生成字段的值

    /*
    * FastJson常用注解
    * 默认将没有值的属性不返回
    * @JSONField(format="yyyy/MM/dd") 将时间转化成指定格式返回
    * @JSONField(name="new_name") 重新定义字段名返回
    * @JSONField(serialize=false) 过滤掉字段不返回
    * @JSONField(serialzeFeatures=SerializerFeature.WriteMapNullValue) null值也返回
    * @JSONField(serialzeFeatures=SerializerFeature.WriteNullStringAsEmpty) 字符串属性若为null,则返回“”
    * @JSONField(serialzeUsing=DoubleSerialize2.class) 自定义规则
    * */
}
